//
//  EntrustPurchaseViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/14.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "EntrustPurchaseViewModel.h"
#import "EntrustPictureTableViewCell.h"
#import "CardlistTableViewCell.h"
#import "AdressSelectTableViewCell.h"

@interface EntrustPurchaseViewModel ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray * leftTitArr;
}
@end

@implementation EntrustPurchaseViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        leftTitArr = @[@"",@"品名",@"材质",@"规格",@"地区"];
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(44);
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        [superView addSubview:tableView];
        //
        GLGWButton * comintBut = [[GLGWButton alloc] initWithFrame:CGRectMake(0, kY_With_View(tableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(44)) titleText:@"提交委托" titleLableFont:SYSTEMFONT(16) titleLableColor:[UIColor whiteColor] inSuperView:superView];
        comintBut.backgroundColor = kColor_Rule_Yellow;
        [[comintBut rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self requestWeiData];
        }];
    }
    return self;
}

#pragma mark --- 数据请求
-(void)requestWeiData{
    
//    if (self.delegate && [self.delegate respondsToSelector:@selector(enturstUpDown)]) {
//        [self.delegate enturstUpDown];
//    }
    NSDictionary * parDic = @{@"userId":koString(self.userId),
                              @"type":@4,
                              @"quoteLimit":@3,
                              @"area":@"北京",
                              @"source":@2,
                              @"productId":@1000,
                              @"productName":@"螺纹钢",
                              @"materialId":@2,
                              @"materialName":@"20#",
                              @"specificationsId":@1,
                              @"specificationsName":@"50*9",
                              @"contacts":@"lijun",
                              @"contactPhone":@"18301698947",
                              @"picUrls":@"http://img.zcool.cn/community/0188915805cc66a84a0e282b933a57.jpg@1280w_1l_2o_100sh.png"};
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kPubpicProxychases] lodingView:nil lodingTextString:@"" isLoding:YES parameters:parDic success:^(id  _Nullable response) {
        
    } failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark -- UITableViewDataSource,UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 5;
    }else{
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return kHeightProportion_In_iPhoneTriploid(232);
    }else{
        return kHeightProportion_In_iPhoneTriploid(44);
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell * cell;
    switch (indexPath.section) {
        case 0:{
            if (indexPath.row == 0) {
                EntrustPictureTableViewCell * cell_entruse = [EntrustPictureTableViewCell cellWithTableView:tableView];
                cell_entruse.Block_ImageView = ^(GLGWImageView * _Nonnull imageView) {
                    if (self.delegate && [self.delegate respondsToSelector:@selector(pictureAction:)]) {
                        [self.delegate pictureAction:imageView];
                    }
                };
                cell = cell_entruse;
            }else{
                CardlistTableViewCell * cell_select = [CardlistTableViewCell cellWithTableView:tableView];
                cell_select.c_type = CellType_Entrust;
                cell_select.nameLeftStr = leftTitArr[indexPath.row];
                cell_select.showPush = YES;
                cell = cell_select;
                [cell setSepartionLineWith:lineModel_Half];
            }
        }
            break;
        case 1:{
            AdressSelectTableViewCell * cell_phone = [AdressSelectTableViewCell cellWithTableView:tableView];
            cell_phone.leftTitString = @"联系人手机号";
            cell_phone.inputTextField.userInteractionEnabled = YES;
            cell_phone.inputTextField.textAlignment = NSTextAlignmentLeft;
            cell_phone.inputTextField.placeholder = @"请填写联系人手机号";
            cell = cell_phone;
            [cell setSepartionLineWith:lineModel_Half];
        }
            break;
            
        default:
            break;
    }
    if (cell == nil) {
        cell = [BaseTableViewCell cellWithTableView:tableView];
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headreView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
    headreView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    return headreView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kHeightProportion_In_iPhoneTriploid(10);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row != 0 && indexPath.row != 4) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(tableSelect:)]) {
            [self.delegate tableSelect:indexPath.row];
        }
    }
}
@end
